﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BaseText;
using Srvtools;

public partial class WorkTime2_WorkTimeOvertime : System.Web.UI.Page
{
    #region 系统

    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitializeComponent();

            Master.DataSource = WMaster;

            MasterPage_MasterPage mpg = (MasterPage_MasterPage)this.Page.Master;
            mpg.HelpHref = "~/Help/WorkTime/加班申请流程.htm";
            mpg.PageName = "加班申请";
            mpg.PicUrl = "";
            mpg.IntiBar();
        }
    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WorkTime2_WorkTimeOvertime));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = false;
        this.WMaster.AlwaysClose = true;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SWorkTime2.WorkTime_OvertimeM";
        this.WMaster.ServerModify = true;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }

    #endregion

    #region 事件

    protected void WebFormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        // ZLTextBox startDate = (ZLTextBox)this.WebFormView1.FindControl("ZLTextBox1");
        // ZLTextBox endDate = (ZLTextBox)this.WebFormView1.FindControl("ZLTextBox2");

        DropDownList Sh = (DropDownList)this.WebFormView1.FindControl("StartHour");
        DropDownList SM = (DropDownList)this.WebFormView1.FindControl("StartMinute");
        DropDownList Eh = (DropDownList)this.WebFormView1.FindControl("EndHour");
        DropDownList Em = (DropDownList)this.WebFormView1.FindControl("EndMinute");

    }


    #endregion

    #region 验证

    private DateTime start = DateTime.MinValue;
    private DateTime end = DateTime.MinValue;

    protected bool ValidateDateFrom(object value)
    {
        start = DateTime.Parse(value.ToString());
        return this.ValidateDate(start, end);
    }

    protected bool ValidateDateTo(object value)
    {
        end = DateTime.Parse(value.ToString());
        return this.ValidateDate(start, end);
    }

    private bool ValidateDate(DateTime start, DateTime end)
    {
        if (start != DateTime.MinValue && end != DateTime.MinValue)
        {
            int sHour = start.Hour;
            int sMinute = start.Minute;
            int eHour = end.Hour;
            int eMinute = end.Minute;

            if (sHour < 8 || sHour == 8 && sMinute < 30 || sHour > 17 || eHour < 8 || eHour == 8 && eMinute < 30 || eHour > 17)
            {
                return false;
            }

            if (start.CompareTo(end) >= 0)
            {
                return false;
            }
        }
        return true;
    }

    #endregion

    #region 自定义方法

    private Single GetHours(string StartHour, string StartMinute, string EndHour, string EndMinute)
    {
        Single OutHour = 0;
        int Dsh = int.Parse(StartHour);
        int DsM = int.Parse(StartMinute);
        int DEh = int.Parse(EndHour);
        int DEM = int.Parse(EndMinute);
        OutHour = Convert.ToSingle(DEh - Dsh) + Convert.ToSingle(DEM - DsM) / 60f;

        return OutHour;

    }

    #endregion

    #region 默认值绑定


    protected object GetOvertimeDate()
    {
        return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).ToShortDateString();
    }

    protected object GetEmployeeId()
    {
        String str = Srvtools.CliUtils.GetValue("_UserCode")[1].ToString();
        str = int.Parse(str).ToString();
        return str;
    }

    #endregion


    protected void WebFormView1_DataBound(object sender, EventArgs e)
    {
        if (WebFormView1.CurrentMode == FormViewMode.Insert || WebFormView1.CurrentMode == FormViewMode.Edit)
        {
            if (Request.Params["MS"] != null)
            {
                WebRefVal tbEmployee = (WebRefVal)this.WebFormView1.FindControl("WebRefVal1");
                if (tbEmployee != null)
                    tbEmployee.ReadOnly = false;
            }
            else
            {
                WebRefVal tbEmployee = (WebRefVal)this.WebFormView1.FindControl("WebRefVal1");
                if (tbEmployee != null)
                    tbEmployee.ReadOnly = true;
            }

        }
    }
    protected void WebFormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        DropDownList Sh = (DropDownList)this.WebFormView1.FindControl("StartHour");
        DropDownList SM = (DropDownList)this.WebFormView1.FindControl("StartMinute");
        DropDownList Eh = (DropDownList)this.WebFormView1.FindControl("EndHour");
        DropDownList Em = (DropDownList)this.WebFormView1.FindControl("EndMinute");

        Single hours = this.GetHours(Sh.SelectedValue, SM.SelectedValue, Eh.SelectedValue, Em.SelectedValue);
        e.NewValues["OvHours"] = hours;

    }
}
